﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ElsProp.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class shuxingbianjiModel : ObservableObject
    {
        /// <summary>
        /// 属性列表
        /// </summary>
        private ShuxingMode _shuxingliebiao;
        /// <summary>
        /// 属性列表
        /// </summary>
        public ShuxingMode shuxingliebiao { get => _shuxingliebiao; set => SetProperty(ref _shuxingliebiao, value); }

        /// <summary>
        /// 添加属性名称
        /// </summary>
        private string _editsxname;
        /// <summary>
        /// 添加属性名称
        /// </summary>
        public string editsxname { get => _editsxname; set => SetProperty(ref _editsxname, value); }


        /// <summary>
        /// 选中的属性
        /// </summary>
        private string _selectShuxing;
        /// <summary>
        /// 选中的属性
        /// </summary>
        public string selectShuxing { get => _selectShuxing; set { SetProperty(ref _selectShuxing, value); editsxname = selectShuxing; } }
        /// <summary>
        /// shuxingzhi
        /// </summary>
        private string _shuxinValue;
        /// <summary>
        /// shuxingzhi
        /// </summary>
        public string shuxinValue { get => _shuxinValue; set => SetProperty(ref _shuxinValue, value); }
        /// <summary>
        /// 单件属性
        /// </summary>
        private ObservableCollection<ItemProperties> _shuxingDanjian;
        /// <summary>
        /// 单件属性
        /// </summary>
        public ObservableCollection<ItemProperties> shuxingDanjian { get => _shuxingDanjian; set => SetProperty(ref _shuxingDanjian, value); }
        /// <summary>
        /// 套装属性
        /// </summary>
        private ObservableCollection<ItemProperties> _shuxingTaoZhuang;
        /// <summary>
        /// 套装属性
        /// </summary>
        public ObservableCollection<ItemProperties> shuxingTaoZhuang { get => _shuxingTaoZhuang; set => SetProperty(ref _shuxingTaoZhuang, value); }
        /// <summary>
        /// 套装条件
        /// </summary>
        private int _TaoZhuangTiaoJian;
        /// <summary>
        /// 套装条件
        /// </summary>
        public int TaoZhuangTiaoJian { get => _TaoZhuangTiaoJian; set => SetProperty(ref _TaoZhuangTiaoJian, value); }

        /// <summary>
        /// 套装名称
        /// </summary>
        private string _suitName;
        /// <summary>
        /// 套装名称
        /// </summary>
        public string suitName { get => _suitName; set => SetProperty(ref _suitName, value); }
        /// <summary>
        /// 套装名称
        /// </summary>
        private string _suitItemName;
        /// <summary>
        /// 套装名称
        /// </summary>
        public string suitItemName { get => _suitItemName; set => SetProperty(ref _suitItemName, value); }
        /// <summary>
        /// 单件名称
        /// </summary>
        private string _danjianName;
        /// <summary>
        /// 单件名称
        /// </summary>
        public string danjianName { get => _danjianName; set => SetProperty(ref _danjianName, value); }



        string filename = "shuxing.json";
        /// <summary>
        /// 添加属性
        /// </summary>
        public ICommand AddSX { get; set; }
        /// <summary>
        /// 删除属性
        /// </summary>
        public ICommand DelSX { get; set; }

        public ICommand AddToDanJain { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ICommand AddToTaoZhuang { get; set; }

        public ICommand DelToDanJain { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ICommand DelToTaoZhuang { get; set; }
        public shuxingbianjiModel()
        {
            AddSX = new RelayCommand(AddShuXinList);
            DelSX = new RelayCommand(DelShuxinList);
            AddToDanJain = new RelayCommand(AddToDanJainMethod);
            AddToTaoZhuang = new RelayCommand(AddToTaoZhuangMethod);
            DelToDanJain = new RelayCommand(DelShuxinList);
            DelToTaoZhuang = new RelayCommand(DelShuxinList);
            shuxingTaoZhuang = new();
            shuxingDanjian = new();
            LoadShuxinList();
        }

        private void AddToTaoZhuangMethod()
        {
            ItemProperties itemProperties = new ItemProperties()
            {
                Condition = TaoZhuangTiaoJian,
                PropertiesName = editsxname,
                PropertiesValue = shuxinValue,
                
            };
            if (TaoZhuangTiaoJian<=1)
            {
                return;
            }
            if (itemProperties.PropertiesName != "" && itemProperties.PropertiesValue != "" && itemProperties.PropertiesValue != "0")
            {
                itemProperties.DeleteThis += ItemProperties_DeleteThis; 
                shuxingTaoZhuang.Add(itemProperties);
            }
        }

        public void ItemProperties_DeleteThis(ItemProperties obj)
        {
            if (obj.Condition>0)
            {
                shuxingTaoZhuang.Remove(obj);
            }
            else
            {
                shuxingDanjian.Remove(obj);
            }
        }


        private void AddToDanJainMethod()
        {
            ItemProperties itemProperties = new ItemProperties()
            {
                Condition = -1,
                PropertiesName = editsxname,
                PropertiesValue = shuxinValue,
            };
            if (itemProperties.PropertiesName != "" && itemProperties.PropertiesValue != "" && itemProperties.PropertiesValue != "0")
            {
                itemProperties.DeleteThis += ItemProperties_DeleteThis;
                shuxingDanjian.Add(itemProperties);
            }
        }

        private void AddShuXinList()
        {
            shuxingliebiao.shuxing.Add(editsxname);
            File.WriteAllText(filename, Newtonsoft.Json.JsonConvert.SerializeObject(shuxingliebiao));
            LoadShuxinList();
        }

        private void DelShuxinList()
        {
            shuxingliebiao.shuxing.Remove(editsxname);
            File.WriteAllText(filename, Newtonsoft.Json.JsonConvert.SerializeObject(shuxingliebiao));
            LoadShuxinList();
        }

        private void LoadShuxinList()
        {
            ShuxingMode shuxingMode = new ShuxingMode();
            if (!File.Exists(filename))
            {
                File.Create(filename).Close();
                shuxingMode.shuxing.Add(Data.pro_攻击体力在100以下的敌人时伤害);
                shuxingMode.shuxing.Add(Data.pro_MP消耗量减少);
                shuxingMode.shuxing.Add(Data.pro_两极化);
                shuxingMode.shuxing.Add(Data.pro_所有技能伤害加);
                shuxingMode.shuxing.Add(Data.pro_所有技能冷却时间减少);
                shuxingMode.shuxing.Add(Data.pro_攻击时3秒内造成攻击伤害的额外伤害);
                shuxingMode.shuxing.Add(Data.pro_暴击);
                shuxingMode.shuxing.Add(Data.pro_极大化);
                shuxingMode.shuxing.Add(Data.pro_适应力);
                File.WriteAllText(filename, Newtonsoft.Json.JsonConvert.SerializeObject(shuxingMode));
            }
            else
            {
                var jsondata = File.ReadAllText(filename);
                shuxingMode = Newtonsoft.Json.JsonConvert.DeserializeObject<ShuxingMode>(jsondata);
            }
            shuxingliebiao = shuxingMode;
        }
    }
}
